﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using Microsoft.SqlServer.Types;
using System.Data.SqlTypes;
using NUnit.Framework;

namespace StorageUnitTests.General
{
    using Storage;
    using Storage.EntityModels;

    #region SetUp class

    /// <summary>
    /// This class contains the set up method that is executed once 
    /// before any of the fixtures contained in this namespace.
    /// </summary>
    [SetUpFixture]
    public class SetUpClass
    {
        /// <summary>
        /// This method is runned before all other tests. 
        /// It is used to get the TripLoaDbEntities.
        /// </summary>
        [SetUp]
        public void SetUpMethod()
        {
            Tests.db = StorageManager.Instance.Entities;
        }
    }

    #endregion

    #region Tests

    /// <summary>
    /// This class contains all the tests for the storage module.
    /// </summary>
    [TestFixture]
    public class Tests
    {
        public static TripLoaDataContext db;

        #region Add tests

        [Test]
        public void TestGetTripById()
        {
            Trip t = StorageManager.Instance.GetTripById(1059);

            foreach (var s in t.Stages)
                Console.Out.WriteLine(s.Ordinal);
        }

        /// <summary>
        /// This method is used to Test the insertion of an User.
        /// </summary>
        [Test]
        [Category("Add")]
        public void TestAddUser()
        {
            User user = TestsHelper.CreateUser();
            db.Users.InsertOnSubmit(user);
            db.SubmitChanges();
            
            // Check 1: an entry (and only one) in Users table must exists. 
            var check1 = from u in db.Users 
                         select u;

            Assert.AreEqual(1, check1.ToArray().Length, 
                "Check 1: an entry (and only one) in Users table must exists.");
            
            /////////////////////////////////////////////////////

            var check2 = from vpl in db.VariablePrivacyLevels
                         select vpl;

            // Check 2.1: in VariablePrivacyLevels must exists only one entry.
            Assert.AreEqual(1, check2.ToArray().Length, 
                "Check 2.1: in VariablePrivacyLevels must exists only one entry.");

            // Check 2.2: an entry related to previously added user must exists in VariablePrivacyLevels.
            Assert.AreEqual(user.Id, check2.First().UserId, 
                "Check 2.2: an entry related to previously added user must exists in VariablePrivacyLevels.");
            
            /////////////////////////////////////////////////////

            var check3 = from uf in db.UserFeedbacks
                         select uf;

            // Check 3.1: in UserFeedbacks must exists only one entry.
            Assert.AreEqual(1, check3.ToArray().Length, 
                "Check 3.1: in UserFeedbacks must exists only one entry.");

            // Check 3.2: an entry related to previously added user must exists in UserFeedbacks.
            Assert.AreEqual(user.Id, check3.First().UserId, 
                "Check 3.2: an entry related to previously added user must exists in UserFeedbacks.");

            /////////////////////////////////////////////////////

            var check4 = from up in db.UserPreferences
                         select up;

            // Check 4.1: in UserPreferences must exists only one entry.
            Assert.AreEqual(1, check4.ToArray().Length, 
                "Check 4.1: in UserPreferences must exists only one entry.");

            // Check 4.2: an entry related to previously added user must exists in UserPreferences.
            Assert.AreEqual(user.Id, check4.First().UserId, 
                "Check 4.2: an entry related to previously added user must exists in UserPreferences.");
        }

        /// <summary>
        /// This method is used to Test the insertion of a Trip.
        /// </summary>
        [Test]
        [Category("Add")]
        public void TestAddTrip()
        {
            //User owner = new User()
            //{
            //    Username = "romiera",
            //    City = "Follonica",
            //    Gender = Gender.Female,
            //    Name = "Romina",
            //    Birthday = new DateTime(1983, 11, 25),
            //    BookedIn = true,
            //    DrivingLicence = true,
            //    Smoker = Smoker.NotSmoker,
            //    Surname = "Paradisi",
            //    Address = "Via Europa",
            //    PhoneNumber = "12345678"
            //};

            //StorageManager.Instance.Entities.Users.InsertOnSubmit(owner);
            //StorageManager.Instance.Entities.SubmitChanges();

            //Trip tt = new Trip
            //{
            //    AcceptSmoker = true,
            //    Accomplished = true,
            //    DepartureDate = new DateTime(2008, 5, 5),
            //    DeviationRadius = 10,
            //    Draft = false,
            //    EstimatedBudget = 30,
            //    EstimatedTime = 3,
            //    FreeSeats = 2,
            //    Owner = owner,
            //    RequireLicenced = true,
            //    ReservationExpirationDate = new DateTime(2008, 5, 4)
            //};

            //StorageManager.Instance.Entities.Trips.InsertOnSubmit(tt);
            //StorageManager.Instance.Entities.SubmitChanges();

            //Stage s = new Stage
            //{
            //    Ordinal = 1,
            //    Origin = new Place()
            //    {
            //        MapPoint = SqlGeography.Point(1.11, 2.67, 4326)
            //    },
            //    Destination = new Place()
            //    {
            //        MapPoint = SqlGeography.Point(5.67, 8.997, 4326)
            //    },
            //    ReservedSeats = 2,
            //    BusySeats = 1,
            //    EstimatedTime = 200,
            //    EstimatedBudget = 1000,
            //    Users_VehicleDetails = new Users_VehicleDetails()
            //    {
            //        VehicleDetails = new VehicleDetails()
            //        {
            //            VehicleClass = VehicleTypes.Car,
            //            Model = "",
            //        },
            //        User = owner
            //    }
            //};

            //s.SetPath("(1.11 2.67), (3.78 4.98), (5.67 8.997)");
            //tt.Stages.Add(s);

            //StorageManager.Instance.Entities.SubmitChanges();


            //var q = from tp in db.Trips
            //        where tp.Id == tt.Id
            //        select tp;

            //Assert.AreEqual(tt, q.First());
        }

        /// <summary>
        /// This method is used to Test the insertion of a Place.
        /// </summary>
        [Test]
        [Category("Add")]
        public void TestAddPlace()
        {
            db.Places.InsertOnSubmit(TestsHelper.CreatePlaceA());
            db.SubmitChanges();

            var q = from p in db.Places
                    select p;

            Assert.AreEqual(1, q.ToArray().Length);
        }

        /// <summary>
        /// Adds two Places Consecutively.
        /// This is a critical situation that sometimes raises errors.
        /// </summary>
        [Test]
        [Category("Add")]
        public void TestAddTwoPlaces()
        {
            db.Places.InsertAllOnSubmit(new[] { TestsHelper.CreatePlaceA(), TestsHelper.CreatePlaceB() });
            db.SubmitChanges();

            var q = from p in db.Places
                    select p;

            Assert.AreEqual(2, q.ToArray().Length);
        }

        /// <summary>
        /// This method is used to Test the insertion of a Stage.
        /// </summary>
        [Test]
        [Category("Add")]
        public void TestAddStage()
        {
            User owner = new User()
            {
                Username = "romiera",
                City = "Follonica",
                Gender = Gender.Female,
                Name = "Romina",
                Birthday = new DateTime(1983, 11, 25),
                BookedIn = true,
                DrivingLicence = true,
                Smoker = Smoker.NotSmoker,
                Surname = "Paradisi",
                Address = "Via Europa",
                PhoneNumber = "12345678"
            };

            StorageManager.Instance.Entities.Users.InsertOnSubmit(owner);
            StorageManager.Instance.Entities.SubmitChanges();

            Stage st = TestsHelper.CreateStage();
            st.Users_VehicleDetails = new Users_VehicleDetails()
            {
                VehicleDetails = new VehicleDetails()
                {
                     VehicleClass = VehicleTypes.Car,
                     Model = "bel modello",
                },
                 User = owner
            };
            db.Stages.InsertOnSubmit(st);
            db.SubmitChanges();

            var q = from s in db.Stages
                    select s;

            Assert.AreEqual(1, q.ToArray().Length);
        }

        /// <summary>
        /// Adds a DrivingInfo.
        /// </summary>
        [Test]
        [Category("Add")]
        public void TestAddDrivingInfo()
        {
            //User owner = new User()
            //{
            //    Username = "romiera",
            //    City = "Follonica",
            //    Gender = Gender.Female,
            //    Name = "Romina",
            //    Birthday = new DateTime(1983, 11, 25),
            //    BookedIn = true,
            //    DrivingLicence = true,
            //    Smoker = Smoker.NotSmoker,
            //    Surname = "Paradisi",
            //    Address = "Via Europa",
            //    PhoneNumber = "12345678"
            //};

            //StorageManager.Instance.Entities.Users.InsertOnSubmit(owner);
            //StorageManager.Instance.Entities.SubmitChanges();

            //Trip tt = new Trip
            //{
            //    AcceptSmoker = true,
            //    Accomplished = true,
            //    DepartureDate = new DateTime(2008, 5, 5),
            //    DeviationRadius = 10,
            //    Draft = false,
            //    EstimatedBudget = 30,
            //    EstimatedTime = 3,
            //    FreeSeats = 2,
            //    Owner = owner,
            //    RequireLicenced = true,
            //    ReservationExpirationDate = new DateTime(2008, 5, 4)
            //};

            //StorageManager.Instance.Entities.Trips.InsertOnSubmit(tt);
            //StorageManager.Instance.Entities.SubmitChanges();

            //Stage s = new Stage
            //{
            //    Ordinal = 1,
            //    Origin = new Place()
            //    {
            //        MapPoint = SqlGeography.Point(1.11, 2.67, 4326)
            //    },
            //    Destination = new Place()
            //    {
            //        MapPoint = SqlGeography.Point(5.67, 8.997, 4326)
            //    },
            //    ReservedSeats = 2,
            //    BusySeats = 1,
            //    EstimatedTime = 200,
            //    EstimatedBudget = 1000,
            //    Users_VehicleDetails = new Users_VehicleDetails()
            //    {
            //        VehicleDetails = new VehicleDetails()
            //        {
            //            VehicleClass = VehicleTypes.Car,
            //            Model = "bel modello",
            //        },
            //        User = owner
            //    }
            //};

            //s.SetPath("(1.11 2.67), (3.78 4.98), (5.67 8.997)");
            //tt.Stages.Add(s);

            //StorageManager.Instance.Entities.SubmitChanges();


            //s.DrivingInfos.Add(new DrivingInfo()
            //{
            //    Info = "Sempre dritto",
            //    MapPoint = SqlGeography.Point(5.67, 8.997, 4326),
            //    InfoOrdinal = 1,
            //    Stage = s,
            //});

            //db.SubmitChanges();

            //var stg = from t in db.Trips
            //          select t.Stages;

            //Assert.AreEqual("Sempre dritto", stg.First().First().DrivingInfos.First().Info);
        }
        
        #endregion

        #region Delete tests

        /// <summary>
        /// Deletes a User, and all his associated data.
        /// </summary>
        [Test]
        [Category("Delete")]
        public void TestDeleteUser()
        {
            // Create two buddies.
            User buddy1 = new User 
            { 
                Username = "buddy1",
                Smoker = Smoker.Smoker, 
                BookedIn = true 
            };

            User buddy2 = new User 
            { 
                Username = "buddy2",
                Smoker = Smoker.Smoker, 
                BookedIn = true 
            };

            // Set up state.
            User u = TestsHelper.CreateUser();
            u.BuddyList.Add(new BuddyList { OwnerUser = u, Buddy = buddy1 });
            u.BuddyList.Add(new BuddyList { OwnerUser = buddy1, Buddy = u });
            db.Users.InsertOnSubmit(u);
            db.SubmitChanges();

            db.Users.DeleteOnSubmit(u);
            db.SubmitChanges();

            // Check user cancellation.
            var check1 = from user in db.Users
                         select user;

            Assert.AreEqual(0, check1.ToArray().Length);
            
            // Check cancellation of related data.
            var check2 = from vpl in db.VariablePrivacyLevels
                         select vpl;
            
            Assert.AreEqual(0, check2.ToArray().Length);

            var check3 = from userFeeback in db.Users
                         select userFeeback;

            Assert.AreEqual(0, check3.ToArray().Length);

            // No entry in the buddy list.
            var check4 = from buddyListEntry in db.BuddyLists
                         where buddyListEntry.Buddy == u || buddyListEntry.OwnerUser == u
                         select buddyListEntry;

            Assert.AreEqual(0, check4.ToArray().Length);
        }

        #endregion

        #region Select tests

        /// <summary>
        /// Creates 4 new User and test their Age is the age expected
        /// </summary>
        [Test]
        [Category("Select")]
        public void TestGetUserAge()
        {
            // creates three users
            User u1 = new User()
            {
                Username = "u1",
                Smoker = Smoker.NotSmoker,
                BookedIn = false,
                Birthday = new DateTime(DateTime.Today.Year - 25, DateTime.Now.Month + 1, DateTime.Now.Day + 1)
            };

            User u2 = new User()
            {
                Username = "u2",
                Smoker = Smoker.NotSmoker,
                BookedIn = false,
                Birthday = new DateTime(DateTime.Today.Year - 26, DateTime.Now.Month, DateTime.Now.Day)
            };

            User u3 = new User()
            {
                Username = "u3",
                Smoker = Smoker.NotSmoker,
                BookedIn = false,
                Birthday = new DateTime(DateTime.Today.Year - 25, DateTime.Now.Month, DateTime.Now.Day + 1)
            };

            User u4 = new User()
            {
                Username = "u4",
                Smoker = Smoker.NotSmoker,
                BookedIn = false,
                Birthday = new DateTime(DateTime.Today.Year - 28, 2, 26)
            };

            db.Users.InsertOnSubmit(u1);
            db.Users.InsertOnSubmit(u2);
            db.Users.InsertOnSubmit(u3);
            db.Users.InsertOnSubmit(u4);
            db.SubmitChanges();

            // Tests the users age

            Assert.AreEqual(24, u1.Age);
            Assert.AreEqual(26, u2.Age);
            Assert.AreEqual(24, u3.Age);
            Assert.AreEqual(28, u4.Age);
        }
        
        /// <summary>
        /// 
        /// </summary>
        [Test]
        [Category("Select")]
        public void TestGetUserByUserName()
        {
            User u = StorageManager.Instance.GetUserByUserName("afuschetto");
            Assert.AreEqual("Antonio", u.Name);
        }

        /// <summary>
        /// Tests
        /// </summary>
        [Test]
        [Category("Select")]
        public void TestGetUserById()
        {
            User u = StorageManager.Instance.GetUserById(1);
            Assert.AreEqual("Antonio", u.Name);
        }


        /// <summary>
        /// Gets the first record in the Places table.
        /// </summary>
        [Test]
        [Category("Select")]
        public void TestGetFirstPlace()
        {
            // Set up state.
            Place placeA = TestsHelper.CreatePlaceA();
            db.Places.InsertOnSubmit(placeA);
            db.SubmitChanges();

            var query = from p in db.Places 
                        select p;
            
            // Only one result.
            Assert.AreEqual(1, query.Count());

            Assert.AreSame(placeA, query.First());
        }

        /// <summary>
        /// 
        /// </summary>
        [Test]
        [Category("Select")]
        public void TestGetUserMessages()
        {
            User receiver = new User()
            {
                Username = "romiera",
                City = "Follonica",
                Gender = Gender.Female,
                Name = "Romina",
                Birthday = new DateTime(1983, 11, 25),
                BookedIn = true,
                DrivingLicence = true,
                Smoker = Smoker.NotSmoker,
                Surname = "Paradisi",
                Address = "Via Europa",
                PhoneNumber = "12345678"
            };

            User sender = new User()
            {
                Username = "superBellissimo",
                City = "Follonica",
                Gender = Gender.Male,
                Name = "Francesco",
                Birthday = new DateTime(1983, 11, 25),
                BookedIn = true,
                DrivingLicence = true,
                Smoker = Smoker.NotSmoker,
                Surname = "Arca",
                Address = "Via Livorno",
                PhoneNumber = "12345678"
            };

            db.Users.InsertOnSubmit(receiver);
            db.Users.InsertOnSubmit(sender);
            db.SubmitChanges();

            receiver.ReceivedMessages.Add(new ReceivedMessages()
            {
                Receiver = receiver,
                Sender = sender,
                IsRead = false,
                Message = new Message()
                {
                     Text = "Ti Amo",
                     Title = "Amore"
                }
            });

            db.SubmitChanges();

            var msg = from m in db.ReceivedMessages
                      where m.ReceiverUserId == receiver.Id
                      select m;

            Assert.AreEqual("Ti Amo", msg.First().Message.Text);
        }

        #endregion

        #region StorageManager tests

        [Test]
        [Category("StorageManager methods")]
        public void TestGetAPageOfSentMessages()
        {
            List<SentMessages> msg = StorageManager.Instance.GetAPageOfSentMessages("romiera", 4, 5);

            foreach (var i in msg)
                Console.Out.WriteLine(i.Message.Text);

            Assert.AreEqual(5, msg.Count);
        }

        [Test]
        public void TestCreateMessage()
        {
            List<string> l = new List<string>();
            l.Add("marcosparagna");
            Message.CreateMessage("ciao", "vado al mare", StorageManager.Instance.GetUserByUserName("romiera").Id, l, false, null, null);
        }

        [Test]
        [Category("StorageManager methods")]
        public void TestGetUserByUserName1()
        {
            User sender = StorageManager.Instance.GetUserByUserName("romiera");
       
            Assert.AreEqual("romiera", sender.Username);
        }

        [Test]
        [Category("StorageManager methods")]
        public void TestMessagesMethods()
        {
        //    User sender = StorageManager.Instance.GetUserByUserName("romiera");

        //    User receiver = StorageManager.Instance.GetUserByUserName("danielegui");

        //    db.SubmitChanges();

        //    Message msg1 = new Message()
        //         {
        //             Text = "Dimmi se ti arriva il messaggio",
        //             Title = "prova invio a daniele GUI",
        //             Date = new DateTime(2008, 5, 21, 14, 48, 10)
        //         };

        //    Message msg2 = new Message()
        //        {
        //            Text = "W il gruppo GUI!",
        //            Title = "Celebrazione",
        //            Date = new DateTime(2008, 5, 21, 14, 48, 10)
        //        };

        //    SentMessages smsg1 = new SentMessages()
        //    {
        //        Message = msg1,
        //        Receiver = receiver,
        //        Sender = sender,
        //    };

        //    sender.SentMessages.Add(smsg1);

        //    SentMessages smsg2 = new SentMessages()
        //    {
        //        Message = msg2,
        //        Receiver = receiver,
        //        Sender = sender,
        //    };

        //    sender.SentMessages.Add(smsg2);

        //    ReceivedMessages rmsg1 = new ReceivedMessages()
        //    {
        //        Message = msg1,
        //        Receiver = receiver,
        //        Sender = sender,
        //    };

        //    receiver.ReceivedMessages.Add(rmsg1);

        //    ReceivedMessages rmsg2 = new ReceivedMessages()
        //    {
        //        Message = msg2,
        //        Receiver = receiver,
        //        Sender = sender,
        //    };

        //    receiver.ReceivedMessages.Add(rmsg2);

        //    Console.Out.WriteLine("pippo");

        //    db.SubmitChanges();

        //    Console.Out.WriteLine("pippo");

        //    List<SentMessages> lsm = StorageManager.Instance.GetAllSentMessagesByUserId(sender.Id);

        //    Console.Out.WriteLine("RECEIVED:");

        //    foreach (var i in lsm)
        //        Console.Out.WriteLine("- " + i.Message.Text);

        //    Assert.AreEqual(2, lsm.Count, "Test failed for GetAllSentMessagesByUserId");

        //    List<ReceivedMessages> lrm = StorageManager.Instance.GetAllReceivedMessagesByUserId(receiver.Id);

        //    Console.Out.WriteLine("SENT:");

        //    foreach (var i in lrm)
        //        Console.Out.WriteLine("- " + i.Message.Text);

        //    Assert.AreEqual(2, lrm.Count, "Test failed for GetReceivedMessageByUserIdAndMsgId");

        //    ReceivedMessages ms1 = StorageManager.Instance.GetReceivedMessageByUserIdAndMsgId(receiver.Id, rmsg1.Id);

        //    Console.WriteLine("MSG1: " + ms1.Message.Text);

        //    List<SentMessages> l_ms2 = StorageManager.Instance.GetSentMessagesByUserIdAndMsgId(sender.Id, smsg1.Id);
        //    foreach (var item in l_ms2)
        //    {
        //        Console.WriteLine("MSG2: " + item.Message.Text + " [Receiver: " + item.ReceiverUserId + "]");
        //    }         

        }

        [Test]
        [Category("StorageManager methods")]
        public void TestAddStageToTrip()
        {
        //    Trip t = TestsHelper.CreateTripForSearch();

        //    Stage s1 = new Stage
        //    {
        //        Ordinal = 2,
        //        Origin = new Place()
        //        {
        //            MapPoint = SqlGeography.Point(45, 25, 4326)
        //        },
        //        Destination = new Place()
        //        {
        //            MapPoint = SqlGeography.Point(41, 5, 4326)
        //        },
        //        ReservedSeats = 2,
        //        BusySeats = 1,
        //        EstimatedTime = 200,
        //        EstimatedBudget = 1000,
        //        Users_VehicleDetails = new Users_VehicleDetails()
        //        {
        //            VehicleDetails = new VehicleDetails()
        //            {
        //                VehicleClass = VehicleTypes.Car,
        //                Model = "bel modello",
        //            },
        //            User = t.Owner
        //        }
        //    };

        //    t.AddStage(s1);
            
        //    s1.SetPath("(45 25), (41 5)");
        //    t.Stages.Add(s1);

        //    db.SubmitChanges();

        //    var q = from i in db.Trips
        //            select i.Stages;

        //    Assert.AreEqual(3, q.Count());
        }

        //DA RIVEDERE IN BASE AL NUVO ID IN STAGE!
        ///// <summary>
        ///// This method is used to Test StorageManager method GetUsersWithReservationRequestByTrip
        ///// and GetUsersWithReservationRequestByTripId.
        ///// </summary>
        ///// 
        //[Test]
        //[Category("StorageManager methods")]
        //public void TestGetReservationRequestByTripId()
        //{
        //    // An User, owner of the trip that will be inserted, is created
        //    User owner = new User()
        //    {
        //        Username = "romiera",
        //        Password = "pwd",
        //        City = "Follonica",
        //        Email = "paradisi@cli.di.unipi.it",
        //        Gender = Gender.Female,
        //        Name = "Romina",
        //        Birthday = new DateTime(1983, 11, 25),
        //        BookedIn = true,
        //        DrivingLicence = true,
        //        Smoker = false,
        //        Surname = "Paradisi",
        //        Address = "Via Europa",
        //        PhoneNumber = "12345678"
        //    };

        //    // An user, that will ask for a reservation in a stage, is created
        //    User request1 = new User()
        //    {
        //        Username = "pippo",
        //        Password = "pwd1",
        //        City = "Pisa",
        //        Email = "salsero@cli.di.unipi.it",
        //        Gender = Gender.Male,
        //        Name = "Pier",
        //        Birthday = new DateTime(1983, 6, 20),
        //        BookedIn = true,
        //        DrivingLicence = true,
        //        Smoker = false,
        //        Surname = "Conti",
        //        Address = "Via Betlemme",
        //        PhoneNumber = "3426287476"
        //    };

        //    // An user, that will ask for a reservation in a stage, is created
        //    User request2 = new User()
        //    {
        //        Username = "pluto",
        //        Password = "pwd2",
        //        City = "Pisa",
        //        Email = "theboss@cli.di.unipi.it",
        //        Gender = Gender.Male,
        //        Name = "Giacomo",
        //        Birthday = new DateTime(1983, 6, 20),
        //        BookedIn = true,
        //        DrivingLicence = true,
        //        Smoker = false,
        //        Surname = "Righetti",
        //        Address = "Via Pollo Caverna",
        //        PhoneNumber = "3426287476"
        //    };

        //    StorageManager.Instance.Entities.Users.InsertOnSubmit(owner);
        //    StorageManager.Instance.Entities.Users.InsertOnSubmit(request1);
        //    StorageManager.Instance.Entities.Users.InsertOnSubmit(request2);
        //    StorageManager.Instance.Entities.SubmitChanges();

        //    // The trip is created
        //    Trip tt = new Trip
        //    {
        //        AcceptSmoker = true,
        //        Accomplished = true,
        //        DepartureDate = new DateTime(2008, 5, 5),
        //        DeviationRadius = 10,
        //        Draft = false,
        //        EstimatedBudget = 30,
        //        EstimatedTime = 3,
        //        FreeSeats = 2,
        //        Owner = owner,
        //        RequireLicenced = true,
        //        ReservationExpirationDate = new DateTime(2008, 5, 4)
        //    };

        //    StorageManager.Instance.Entities.Trips.InsertOnSubmit(tt);
        //    StorageManager.Instance.Entities.SubmitChanges();

        //    // Two stages are created and added to the trip
        //    Stage s = new Stage
        //    {
        //        Ordinal = 1,
        //        Origin = new Place()
        //        {
        //            MapPoint = SqlGeography.Point(1.11, 2.67, 4326)
        //        },
        //        Destination = new Place()
        //        {
        //            MapPoint = SqlGeography.Point(5.67, 8.997, 4326)
        //        },
        //        ReservedSeats = 2,
        //        BusySeats = 1,
        //        EstimatedTime = 200,
        //        EstimatedBudget = 1000,
        //        Users_VehicleDetailsId = 1,
        //        Users_VehicleDetails = new Users_VehicleDetails()
        //        {
        //            User = owner,
        //            VehicleDetails = new VehicleDetails()
        //                {
        //                    VehicleClass = VehicleTypes.Car,
        //                    Model = "cazzo"
        //                }
        //        }
        //    };

        //    Stage s1 = new Stage
        //    {
        //        Ordinal = 2,
        //        Origin = new Place()
        //        {
        //            MapPoint = SqlGeography.Point(1.11, 2.67, 4326)
        //        },
        //        Destination = new Place()
        //        {
        //            MapPoint = SqlGeography.Point(5.67, 8.997, 4326)
        //        },
        //        ReservedSeats = 2,
        //        BusySeats = 1,
        //        EstimatedTime = 200,
        //        EstimatedBudget = 1000,
        //        Users_VehicleDetailsId = 1,
        //        Users_VehicleDetails = new Users_VehicleDetails()
        //        {
        //            User = owner,
        //            VehicleDetails = new VehicleDetails()
        //                {
        //                    VehicleClass = VehicleTypes.Car,
        //                    Model = "abc"
        //                }
        //        }
        //    };
        //    s.SetPath("(1.11 2.67), (3.78 4.98), (5.67 8.997)");
        //    s1.SetPath("(1.11 2.67), (3.78 4.98), (5.67 8.997)");
        //    // The users created before are added to the ReservationRequest list af the stages created
        //    s.ReservationRequests.Add(new ReservationRequest()
        //    {
        //        StageId = s.Id,
        //        Stage = s,
        //        User = request1
        //    });
        //    s1.ReservationRequests.Add(new ReservationRequest()
        //    {
        //        StageId = s.Id,
        //        Stage = s,
        //        User = request1
        //    });

        //    s1.ReservationRequests.Add(new ReservationRequest()
        //    {
        //        Ordinal = 2,
        //        Stage = s,
        //        User = request2
        //    });
            
        //    tt.Stages.Add(s);
        //    tt.Stages.Add(s1);

        //    StorageManager.Instance.Entities.SubmitChanges();

        //    // Tests the right output of our methods
        //    var q = from tp in db.Trips
        //            where tp.Id == tt.Id
        //            select tp;

        //    Assert.AreEqual(2, StorageManager.Instance.GetUsersWithReservationRequestByTripId(q.First().Id).Count);
        //    Assert.AreEqual(2, StorageManager.Instance.GetUsersWithReservationRequestByTrip(q.First()).Count);
        //}

        /// <summary>
        /// Tests that the method GetVisibleField gets 
        /// all the visible fields for a specified user
        /// </summary>
        /// 
        [Test]
        [Category("StorageManager methods")]
        public void TestGetVisibleFields()
        {
            /*User user = new User 
            {
                VariablePrivacyLevels = VariablePrivacyLevels.CreateVariablePrivacyLevels()
            };

            var result = StorageManager.Instance.GetVisibleFields(1, PrivacyLevel.High);*/

            // FIX: a partire dalla politica di filtraggio verificare che solo i dati effettivamente
            // visibili lo siano!
        }


        /// <summary>
        /// Tests if the right relation between two specified users 
        /// is returned by the method GetRelation
        /// </summary>
        [Test]
        [Category("StorageManager methods")]
        public void TestGetRelation()
        {
            /*PrivacyLevel pvl_test = PrivacyLevel.NotVisible;
            pvl_test = StorageManager.Instance.GetRelation(60, 97);
            Console.WriteLine(pvl_test.ToString());
            Console.WriteLine(pvl_test.ToString());
            Console.WriteLine(pvl_test.ToString());*/

            /*User giacomo = User.CreateUser("kirkrulez", "pwdRighetti", "Giacomo", "Righetti",
                true, new DateTime(1983, 7, 10), "Sarzana", "via Pisa", "righetti@righetti.ri", "12345678", true, false);

            User romina = User.CreateUser("romiera", "pwdParadisi", "Romina", "Paradisi",
                false, new DateTime(1983, 11, 25), "Follonica", "via Lucca", "paradisi@paradisi.pa", "87654321", true, false);

            BuddyList u1 = new BuddyList();
            u1.Users = giacomo;
            u1.Users1 = romina;

            db.SaveChanges();*/

            //PrivacyLevel p = StorageManager.Instance.GetRelation(giacomo.Id, romina.Id);

            //Assert.AreEqual(PrivacyLevel.Low, p);
        }

        #endregion

        #region Dummy test

        /// <summary>
        /// Empty test used to test inizialization of StorageManager.
        /// </summary>
        [Test]
        public void DummyTest()
        {
        }

        #endregion

        #region SetUp

        /// <summary>
        /// Executed before every test.
        /// </summary>
        [SetUp]
        public void SetUpMethod()
        {
            TestsHelper.CleanUpDB(db);
        }

        #endregion
    }

    #endregion
}
